import { api, METHOD } from '../utils/register-route';
var mongoose = require('mongoose');
require('../utils/sequelize');
import {insertTeacher,findTeacher,findOneTeacher,removeTeacher,removeAllTeacher,updateTeacher} from '../models/SYS_Teacher'

var UUID = require('uuid');

export default class SYS_Teacher {
	/**
	 * 教师列表
	 * /SYS_Teacher/
	 */
	@api({
		method: METHOD.GET,
	})
	public async index(ctx: any) {
		const body = ctx.request.query;
		let parm = {};
        if(!!body.Pro){
          parm["Pro"] = body.Pro
        }
        if(!!body.City){
          parm["City"] = body.City
        }
        if(!!body.TargetStudent){
		  		parm["TargetStudent"] = body.TargetStudent
        }
        if(!!body.Sex){
          parm["Sex"] = body.Sex
        }
        if(!!body.Price){
          parm["Price"] = {$lte:body.Price}
        }
        if(!!body.Age){
          parm["Age"] = {$lte:body.Age}
        }
        if(!!body.Name){
          let reg = new RegExp(body.Name, 'i') //名称模糊查询
          parm["Name"] = {$regex : reg};
		}

		let page = 0;
		if(!!body.page){
			page = body.page - 1;
		}

		
		var pets;
		try{
			pets = await findTeacher(parm,page);
			ctx.res.success(pets);
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

	/**
	 * 单个教师
	 * /SYS_Teacher/single
	 */
	@api({
		method: METHOD.GET,
	})
	public async single(ctx: any) {
		const body = ctx.request.query;
		var pets;
		try{
			pets = await findOneTeacher({Id:body.Id});
			if(pets != null){
				ctx.res.success(pets);
			}else{
				ctx.res.failure("没有查询到数据");
			}
			
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

	/**
	 * 登陆
	 * /SYS_Teacher/login
	 */
	@api({
		method: METHOD.GET,
	})
	public async login(ctx: any) {
		const body = ctx.request.query;
		let Tel = body.Tel;
		let Password = body.Password;
		let parm = {
			Tel:Tel,
			Password:Password
		}
		var pets;
		try{
			pets = await findOneTeacher(parm);
			if(pets != null){
				ctx.res.success(pets);
			}else{
				ctx.res.failure("用户名或密码不正确");
			}
			
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

	/**
	 * 教师新增
	 * /SYS_Teacher/
	 */
	@api({
		method: METHOD.POST,
	})
	public async add(ctx: any) {
		const body = ctx.request.body;
		//判断手机号是否已经存在
		let Tel = body.Tel;
		pets = await findOneTeacher({Tel:Tel});
		if(pets != null){
			ctx.res.failure("手机号已经被使用");
			return;
		}

		let options = {
			Id:UUID.v1(),
			ClassId:UUID.v1(),
		};
		Object.assign(options,body)

		var pets;
		try{
			pets = await insertTeacher(options)
			ctx.res.success(pets);
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

	/**
	 * 删除教师
	 * /SYS_Teacher/
	 */
	@api({
		method: METHOD.GET,
	})
	public async del(ctx: any) {
		const body = ctx.request.query;
		var pets;
		try{
			pets = await removeTeacher(body);
			ctx.res.success(pets);
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

	/**
	 * 删除所有教师
	 * /SYS_Teacher/
	 */
	@api({
		method: METHOD.GET,
	})
	public async delAll(ctx: any) {
		const body = ctx.request.query;
		var pets;
		try{
			pets = await removeAllTeacher();
			ctx.res.success(pets);
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}


	/**
	 * 修改教师
	 * /SYS_Teacher/
	 */
	@api({
		method: METHOD.POST,
	})
	public async update(ctx: any) {
		const body = ctx.request.body;
		var pets;
		try{
			pets = await updateTeacher(body);
			ctx.res.success(pets);
		}catch (err){
			pets = err;
			ctx.res.failure(pets);
		}
	}

}
